/**
 * 
 */
package junior.db.dao.base;

import junior.db.dao.query.AbstractQuery;
import io.swagger.annotations.ApiModelProperty;

/**
 * <h3>排序</h3>
 * <p>一般不直接使用此类，而是使用{@link AbstractQuery}的asc和desc方法</p>
 * @author bo.wu
 * @CreateDate 2014年7月10日
 */
public class Sort {
    @ApiModelProperty(hidden = true)
	private transient String prefix;
    @ApiModelProperty(hidden = true)
    private transient boolean compression;
	private String field;
	private boolean asc;
	
	public static Sort asc(String field) {
		return new Sort(field, true);
	}
	
	public static Sort desc(String field) {
		return new Sort(field, false);
	}
	
	public Sort() {}
	
	public Sort(String field, boolean asc) {
		int idx = field.indexOf(".");
		if (idx != -1) {
			this.prefix = field.substring(0,  idx + 1);
			this.field = field.substring(idx + 1);
		} else {
			this.field = field;
		}
		this.asc = asc;
	}
	
	public String getField() {
		return field;
	}
	
	public void setField(String field) {
		this.field = field;
	}
	
	public boolean isAsc() {
		return asc;
	}
	
	public void setAsc(boolean asc) {
		this.asc = asc;
	}
    
    public boolean isCompression() {
        return compression;
    }
    
    public Sort withCompression() {
        this.compression = true;
        return this;
    }
    
    public String getPrefix() {
		return prefix != null ? prefix : "";
	}
	
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
	
	@Override
	public String toString() {
		return getPrefix() + getField() + (isAsc() ? " asc" : " desc");
	}
}
